﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using Computer_Manager.BUS;
using Computer_Manager.DTO;
using DevExpress.XtraGrid.Views.Grid;

namespace Computer_Manager.GUI
{
    public partial class frmProduct : DevExpress.XtraEditors.XtraForm
    {
        BUS_categories bus_cat = new BUS_categories();
        BUS_product bus_pro = new BUS_product();
        BUS_order_detail bus_or_de = new BUS_order_detail();
        DTO_categories dto_cat = new DTO_categories();
        DTO_product dto_pro = new DTO_product();
        DTO_order_detail dto_or_de = new DTO_order_detail();

        public frmProduct()
        {
            InitializeComponent();
        }

        private void frmProduct_Load(object sender, EventArgs e)
        {
            dto_cat.parent_id = 0;
            DataTable nodecha = new DataTable();
            DataTable nodecon = new DataTable();
            DataTable nodecon1 = new DataTable();
            dto_cat.parent_id = 0;
            nodecha = bus_cat.GetByParentId(dto_cat);
            treeCate.CollapseAll();
            for (int i = 0; i < nodecha.Rows.Count; i++)
            {
                treeCate.Nodes.Add(nodecha.Rows[i][2].ToString());
                treeCate.Nodes[i].Tag = nodecha.Rows[i][0].ToString();
                dto_cat.parent_id = int.Parse(nodecha.Rows[i][0].ToString());
                nodecon = bus_cat.GetByParentId(dto_cat);
                for (int j = 0; j < nodecon.Rows.Count; j++)
                {
                    treeCate.Nodes[i].Nodes.Add(nodecon.Rows[j][2].ToString());
                    treeCate.Nodes[i].Nodes[j].Tag = nodecon.Rows[j][0].ToString();
                    dto_cat.parent_id = int.Parse(nodecon.Rows[j][0].ToString());
                    nodecon1 = bus_cat.GetByParentId(dto_cat);
                    for (int k = 0; k < nodecon1.Rows.Count; k++)
                    {
                        treeCate.Nodes[i].Nodes[j].Nodes.Add(nodecon1.Rows[k][2].ToString());
                        treeCate.Nodes[i].Nodes[j].Nodes[k].Tag = nodecon1.Rows[k][0].ToString();
                    }
            }
            loaddata();
            grvProduct.OptionsView.NewItemRowPosition = NewItemRowPosition.None;
            grvProduct.OptionsBehavior.ReadOnly = true;
            grvProduct.OptionsBehavior.Editable = false;
            grvProduct.Columns[0].OptionsFilter.AutoFilterCondition = DevExpress.XtraGrid.Columns.AutoFilterCondition.Contains;
            grvProduct.Columns[1].OptionsFilter.AutoFilterCondition = DevExpress.XtraGrid.Columns.AutoFilterCondition.Contains;
            grvProduct.Columns[2].DisplayFormat.FormatType = DevExpress.Utils.FormatType.Custom;
            grvProduct.Columns[2].DisplayFormat.FormatString = "{0:0,0 vnđ}";
            grvProduct.Columns[4].DisplayFormat.FormatType = DevExpress.Utils.FormatType.Custom;
            grvProduct.Columns[4].DisplayFormat.FormatString = "{0:0,0 vnđ}";
            }
            //EventGridview();
        }

        public void loaddata() 
        {
            //dto_pro.id_cat = value;
            grcProduct.DataSource = bus_pro.GetAll(); 
            lblCount.Text = "Có: "+ bus_pro.GetAll().Rows.Count.ToString() + " sản phẩm";
        }

        private void treeCate_AfterSelect(object sender, TreeViewEventArgs e)
        {
            treeCate.SelectedNode.Expand();   
            EventGridview();
            if (treeCate.SelectedNode.Level == 0)
            {
                dto_cat.parent_id = int.Parse(treeCate.SelectedNode.Tag.ToString());
                grcProduct.DataSource = bus_cat.GetByParentCat(dto_cat);
                lblCount.Text = "Có: " + bus_cat.GetByParentCat(dto_cat).Rows.Count.ToString() + " sản phẩm";
            }
            else
            {
                dto_pro.id_cat = int.Parse(treeCate.SelectedNode.Tag.ToString());
                grcProduct.DataSource = bus_pro.GetByCat(dto_pro);
                lblCount.Text = "Có: " + bus_pro.GetByCat(dto_pro).Rows.Count.ToString() + " sản phẩm"; 
            }
            ///Nút xoá và sửa
            if (grvProduct.SelectedRowsCount == 0)
            {
                btnEditProduct.Enabled = false;
                btnDelete.Enabled = false;
            }
            else
            {
                btnEditProduct.Enabled = true;
                btnDelete.Enabled = true;
            }
        }

        private void btnAddProduct_Click(object sender, EventArgs e)
        {
            frmAddProduct frmAddProduct = new frmAddProduct();
            frmAddProduct.ShowDialog();
        }

        private void grvProduct_ValidateRow(object sender, DevExpress.XtraGrid.Views.Base.ValidateRowEventArgs e)
        {
            GridView view = sender as GridView;
            if (string.IsNullOrEmpty(view.GetRowCellValue(e.RowHandle, view.Columns[1]).ToString()) || string.IsNullOrEmpty(view.GetRowCellValue(e.RowHandle, view.Columns[2]).ToString()) || string.IsNullOrEmpty(view.GetRowCellValue(e.RowHandle, view.Columns[3]).ToString()) || string.IsNullOrEmpty(view.GetRowCellValue(e.RowHandle, view.Columns[4]).ToString()) || string.IsNullOrEmpty(view.GetRowCellValue(e.RowHandle, view.Columns[5]).ToString()) || string.IsNullOrEmpty(view.GetRowCellValue(e.RowHandle, view.Columns[0]).ToString()))
            {
                XtraMessageBox.Show("Bạn chưa nhập đầy đủ thông tin!");
            }
            else
	        {
                if (view.IsNewItemRow(e.RowHandle))
                {
                    dto_pro.codes_pro = view.GetRowCellValue(e.RowHandle, view.Columns[0]).ToString();
                    dto_pro.name_pro = view.GetRowCellValue(e.RowHandle, view.Columns[1]).ToString();
                    dto_pro.price_in = view.GetRowCellValue(e.RowHandle, view.Columns[2]).ToString();
                    dto_pro.amount_in = int.Parse(view.GetRowCellValue(e.RowHandle, view.Columns[3]).ToString());
                    dto_pro.price_out = view.GetRowCellValue(e.RowHandle, view.Columns[4]).ToString();
                    DateTime ngay = new DateTime();
                    ngay = Convert.ToDateTime(view.GetRowCellValue(e.RowHandle, view.Columns[5]).ToString());
                    dto_pro.date_in = ngay;
                    dto_pro.id_cat = int.Parse(treeCate.SelectedNode.Tag.ToString());
                    bus_pro.product_insert(dto_pro);
                    XtraMessageBox.Show("Thêm thành công!!!");
                    grcProduct.DataSource = bus_pro.GetByCat(dto_pro);
                }
                else
                {
                    try
                    {
                        dto_pro.id_pro = int.Parse(grvProduct.GetFocusedDataRow()["id_pro"].ToString());
                    }
                    catch (Exception){}
                    dto_pro.codes_pro = view.GetRowCellValue(e.RowHandle, view.Columns[0]).ToString();
                    dto_pro.name_pro = view.GetRowCellValue(e.RowHandle, view.Columns[1]).ToString();
                    dto_pro.price_in = view.GetRowCellValue(e.RowHandle, view.Columns[2]).ToString();
                    dto_pro.amount_in = int.Parse(view.GetRowCellValue(e.RowHandle, view.Columns[3]).ToString());
                    dto_pro.price_out = view.GetRowCellValue(e.RowHandle, view.Columns[4]).ToString();
                    DateTime ngay = new DateTime();
                    ngay = Convert.ToDateTime(view.GetRowCellValue(e.RowHandle, view.Columns[5]).ToString());
                    dto_pro.date_in = ngay;
                    dto_pro.id_cat = int.Parse(treeCate.SelectedNode.Tag.ToString());
                    bus_pro.product_Update(dto_pro);
                    XtraMessageBox.Show("Sửa thành công!!!");
                }
	        }
            
        }

        public void EventGridview() 
        {
            try
            {
                if (treeCate.SelectedNode.Level == 0)
                {
                    grvProduct.OptionsView.NewItemRowPosition = NewItemRowPosition.None;
                    grvProduct.OptionsBehavior.ReadOnly = true;
                    grvProduct.OptionsBehavior.Editable = false;
                }
                else
                {
                    grvProduct.OptionsView.NewItemRowPosition = NewItemRowPosition.Top;
                    grvProduct.OptionsBehavior.ReadOnly = false;
                    grvProduct.OptionsBehavior.Editable = true;
                }
            }
            catch (Exception){}
            
        }

        private void btnEditProduct_Click(object sender, EventArgs e)
        {
            frmEditProduct frmEditPRo = new frmEditProduct();
            frmEditPRo.Id_pro = int.Parse(grvProduct.GetFocusedDataRow()["id_pro"].ToString());
            frmEditPRo.ShowDialog();
        }

        private void btnDelete_Click(object sender, EventArgs e)
        {
            dto_pro.id_pro = int.Parse(grvProduct.GetFocusedDataRow()["id_pro"].ToString());
            dto_or_de.id_product = int.Parse(grvProduct.GetFocusedDataRow()["id_pro"].ToString());
            if (bus_or_de.GetByProductId(dto_or_de).Rows.Count > 0)
            {
                XtraMessageBox.Show("Sản phẩm này đã nằm trong hoá đơn nên không thể xoá!","Lỗi",MessageBoxButtons.OK,MessageBoxIcon.Error);
            }
            else
            {
                if (XtraMessageBox.Show("Bạn có chắc muốn xoá không", "Chú ý", MessageBoxButtons.YesNo, MessageBoxIcon.Warning) == DialogResult.Yes)
                {
                    try
                    {
                        bus_pro.product_Delete(dto_pro);
                        loaddata();
                        XtraMessageBox.Show("Xoá thành công!!!");
                    }
                    catch (Exception)
                    {

                        throw;
                    }
                }
            }
           
        }

        private void simpleButton3_Click(object sender, EventArgs e)
        {
            loaddata();
            //MessageBox.Show(grvProduct.GetFocusedDataRow()["id_pro"].ToString());
            if (grvProduct.SelectedRowsCount == 0)
            {
                btnEditProduct.Enabled = false;
                btnDelete.Enabled = false;
            }
            else
            {
                btnEditProduct.Enabled = true;
                btnDelete.Enabled = true;
            }
        }
    }
}